﻿var Global_FormMain = "formMain";

var Global_UI =
{
    AjaxPOST:
    function Global_AjaxPOST(url, data, callBackSuccess, callBackError) {
        $.ajax
        ({
            type: "POST",
            url: url,
            data: data,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: callBackSuccess,
            error: callBackError
        });
    },

    EliminarFilasTabla:
    function Global_EliminarFilasTabla(tabla) {
        $("#" + tabla + " tr").each
        (
            function () {
                $(this).remove();
            }
        );
    },

    JqueryModalForm:
    function Global_JqueryModalForm(modal, alto, ancho, callBack) {
        if (callBack == null) callBack = function () { };

        $("#" + modal).dialog
        ({
            autoOpen: false,
            height: alto,
            width: ancho,
            modal: true,
            position: ['center', 'center'],
            close: callBack
        });
    },

    JqueryModalFormOpen:
    function Global_JqueryModalFormOpen(modal) {
        var modal = ("#" + modal);
        var form = ("#" + Global_FormMain);

        $(modal).dialog("open");

        $(".ui-widget-overlay.ui-front").appendTo($(form));
        $(modal).parent().appendTo($(form));
    },

    ValidateToolTipster:
    function Global_ValidateToolTipster(containerValidate) {
        if (containerValidate == null) containerValidate = Global_FormMain;

        $("#" + containerValidate).validate
        ({
            // any other options & rules,
            errorPlacement: function (error, element) {
                $(element).tooltipster("update", $(error).text());
                $(element).tooltipster("show");
            }
            , success: function (label, element) {
                $(element).tooltipster("hide");
            }
            , onsubmit: false
        });

        Global_GroupsTooltipster(containerValidate);
    },

    GroupsTooltipster:
    function Global_GroupsTooltipster(container) {
        Global_GroupTooltipster(container, "required");

        Global_GroupTooltipster(container, "regularExpression");
    },


    GroupTooltipster:
    function Global_GroupTooltipster(container, group) {
        $("#" + container + " ." + group).tooltipster
        ({
            trigger: "custom", // default is 'hover' which is no good here
            onlyOne: false,    // allow multiple tips to be open at a time
            position: "right",  // display the tips to the right of the element
            timer: 3000
        });
    },

    FiltrarFilasTablaHTML:
    function Global_FiltrarFilasTablaHTML(container, indiceColumna, controlSearch) {
        var controlSearch = $("#" + controlSearch);

        var textoBuscar = controlSearch.val().toLowerCase();

        if (textoBuscar == "") {
            //Si es vacio muestro todas las filas
            $("#" + container + " tr").each
            (
                function () {
                    $(this).show();
                }
            );

            return;
        }

        //Si es diferente de vacio hago el filtro
        $("#" + container + " tr").each
        (
            function () {
                if ($(this).find("td").eq(indiceColumna).html().toLowerCase().indexOf(textoBuscar) >= 0)
                    $(this).show();
                else
                    $(this).hide();
            }
        );
    },

    JqueryValidateGroup:
    function Global_JqueryValidateGroup(validationGroup, container) {
        if (container == null) container = Global_FormMain;

        var isValid = true;

        $("#" + container + " ." + validationGroup).each
        (
            function () {
                if (!$(this).valid()) isValid = false;
            }
        )

        return isValid;
    },

    JqueryValidateFormSubmitFalse:
    function Global_JqueryValidateFormSubmitFalse(form) {
        if (form == null) form = "formMain";

        form = $("#" + form);

        if (form == null) return;

        form.validate({ onsubmit: false });
    },

    ResponseRedirect:
    function Global_ResponseRedirect(pagina) {
        window.location = pagina;
    },

    IsChecked:
    function Global_IsChecked(control) {
        control = $("#" + control);

        if (control == null) return false;

        return (control.is(":checked") ? true : false);
    },

    VacioEsNull:
    function Global_VacioEsNull(control) {
        control = $("#" + control);

        if (control == null) return null;

        var valor = control.val().trim()

        if (valor == "") return null;

        return valor;
    },

    BlockUI:
    function Global_BlockUI(visible) {
        var div = "PUBLIC_divBlockUIProcesando";

        if (visible) 
        {
            var body = $("body");
            
            var exists = document.getElementById(div);

            if(exists != null) return;

            body.append
            (
                "<div class='divBlockUI' id='" + div + "' >" +
                    "<div> <br /> <b>Procesando Informacion</b> <br /> <img alt='' src='../../Imagenes/Loader.gif'/> <br /> </div> " +
                "</div>"
            );
        }
        else 
        {
            div = $("#" + div);

            if (div == null) return;

            div.remove();
        }

        //    var div = "divBlockUIProcesando";

        //    div = $("#" + div);

        //    if (div == null) return;

        //    div.css("display", (visible ? "" : "none"));
    },

    CheckearTodos:
    function Global_CheckearTodos(control, tabla, prefijo) {
        var v = $("#" + control).attr("checked");

        $("#" + tabla + " :checkbox").filter("[id^=" + prefijo + "]").attr("checked", v);
    },

    DialogClose:
    function Global_DialogClose(div) {
        div = $("#" + div);
        div.dialog("close");
        div.remove();
    },

    Alert:
    function Global_Alert(mensaje, titulo, callBack) {
        var body = $("body");
        var divId = "PUBLIC_dialogAlert";

        if (body == null) return;

        if (titulo == null) titulo = "";
        if (callBack == null) callBack = function () { };

        body.append
        (
            "<div id='" + divId + "' title='" + titulo + "'>" +
                "<span class='ui-icon ui-icon-circle-check' style='float: left; margin: 0 7px 50px 0;'></span>" +
                mensaje +
            "</div>"
        );

        var divAlert = $("#" + divId);

        divAlert.dialog
        ({
            resizable: false,
            closeOnEscape: false,
            modal: true,
            open: function () { $(this).parent().children().children('.ui-dialog-titlebar-close').hide(); },
            buttons:
            {
                "Aceptar":
                function () {
                    var callbacks = $.Callbacks("once");

                    callbacks.add(callBack);
                    callbacks.add(function () { Global_DialogClose(divId); });

                    callbacks.fire();
                }
            }
        })
    },

    Confirm:
    function Global_Confirm(pregunta, titulo, callBackOk, callBackCancel) {
        var body = $("body");
        var divId = "PUBLIC_dialogConfirm";

        if (body == null) return;

        if (titulo == null) titulo = "";
        if (callBackOk == null) callBackOk = function () { };
        if (callBackCancel == null) callBackCancel = function () { };

        body.append
        (
            "<div id='" + divId + "' title='" + titulo + "'>" +
                "<p><span class='ui-icon ui-icon-alert' style='float: left; margin: 0 7px 50px 0;'></span>" +
                pregunta + "</p>" +
            "</div>"
        );

        var divConfirm = $("#" + divId);

        divConfirm.dialog
        ({
            resizable: false,
            closeOnEscape: false,
            modal: true,
            open: function () { $(this).parent().children().children('.ui-dialog-titlebar-close').hide(); },
            buttons:
            {
                "Aceptar":
                function () {
                    var callbacks = $.Callbacks("once");

                    callbacks.add(callBackOk);
                    callbacks.add(function () { Global_UI.DialogClose(divId); });

                    callbacks.fire();
                },
                "Cancelar":
                function () {
                    var callbacks = $.Callbacks("once");

                    callbacks.add(callBackCancel);
                    callbacks.add(function () { Global_UI.DialogClose(divId); });

                    callbacks.fire();
                }
            }
        })
    },

    Datepicker:
    function Global_Datepicker(control, imgButton) {
        if (imgButton == null) imgButton = false;

        control = $("#" + control);

        if (!imgButton) {
            control.datepicker
            ({
                changeMonth: true,
                changeYear: true
            });
        }
        else {
            control.datepicker
            ({
                changeMonth: true,
                changeYear: true,
                showOn: "button",
                buttonImage: "../../../Imagenes/IconoCalendario.png",
                buttonImageOnly: true
            });
        }
    },

    Menu:
    function Global_Menu(control) {
        control = $("#" + control);

        control.menu();
    },

    FileUpload:
    function Global_FileUpload(functionCallBack) {
        var iframe = $(".iframeFileUpload");

        if (iframe == null) return;

        iframe.attr("id", "iframeFileUpload");

        $(iframe).focus();

        var fileUpload = $(iframe).contents().find("#PUBLIC_fileUpload");

        if (fileUpload == null) return;

        var callBack = $(iframe).contents().find("#PUBLIC_hiddenCallBack");

        callBack.val(functionCallBack);

        fileUpload.click();
    },

    SendFileUpload:
    function Global_SendFileUpload() {
        var iframe = $(".iframeFileUpload");

        $(iframe).focus();

        var fileUpload = $(iframe).contents().find("#PUBLIC_fileUpload");

        if (fileUpload.val() == "") return;

        var hiddenId = $(iframe).contents().find("#PUBLIC_hiddenIdFileUpload");

        if (hiddenId == null) return;

        hiddenId.val("");

        Global_BlockUI(true);

        iframe = iframe.attr("id");

        document.getElementById(iframe).contentWindow.SubmitFileUpload();
    },

    LoadComboBox:
    function Global_LoadComboBox(items) {
        return $("<select>",{}).append
        (
            $.map
            (
                items,
                function (item) 
                {
                    return $("<option>", { text: item });
                }
            )
        );
    }
}